<?php

/*
 * Fresns Control Panel (Standalone)
 * Copyright (C) 2021-Present 刘鑫
 * Released under the MIT License.
 */

namespace App\Plugins\FcpApi\TweetAdmin;

// 管理员模型配置
use App\Base\Config\BaseConfig;
use Illuminate\Validation\Rule;

class FsConfig extends BaseConfig
{
    // 重置密码
    const RESET_PASSWORD = 'resetPassword';

    // 更新密码
    const UPDATE_PASSWORD = 'updatePassword';

    //登录
    const ADMIN_SING_IN = 1;
    //退出
    const ADMIN_LOGOUT = 2;

    // 新增规则
    public function storeRule()
    {
        $arr = [
            'a' => 1,
            'b' => [
                ['id' => 1, 'name' => 'name1'],
                ['id' => 2, 'name' => 'name2'],
            ],
        ];

        //  dd(json_encode($arr));

        $storeRule = [
            'login_name'    => 'required|min:2|unique:users,login_name',
            'password'      => 'required|min:6',

            'rank_num'      => 'numeric',

            'file_id'   => 'exists:asset_file,id',
            'file_url'  => 'url',
            'email'     => 'email',
            'phone'     => 'min:8|max:15',
            'coin'      => 'numeric',
            'birthday'  => 'date',
            'more_json' => 'json',
            // 'remark'    =>'remark',
            'gender'    => Rule::in(array_keys(BaseConfig::GENDER_MAP)),

        ];

        return $storeRule;
    }

    // 更新规则
    public function updateRule()
    {
        // 唯一性判断不判断当前id

        $updateRule = [
            'id'    => 'required|exists:users',
            // 'login_name'    => [
            //     'required',
            //     Rule::unique('users')->where(function ($query) {
            //         $query->where('deleted_at', null);
            //     })
            // ],
        ];

        return $updateRule;
    }

    // 规则 - 重置密码
    public function resetPasswordRule()
    {
        $rule = [
            'id'    => 'required|exists:users',
            'newPassword'       => 'required|min:6',
            'confirmPassword'   => 'required|min:6|same:newPassword',
        ];

        return $rule;
    }

    // 规则 - 更新密码
    public function updatePasswordRule()
    {
        $rule = [
            'newPassword'       => 'required|min:6',
            'confirmPassword'   => 'required|min:6|same:newPassword',
            'oldPassword'   => 'required|min:6',
        ];

        return $rule;
    }

    // 规则 - store 验证 more_json
    public function storeMoreJsonRule()
    {
        $rule = [
            'a'   => 'filled',
            'b'   => 'filled|array',
            'b.*.id'   => 'filled|min:2',
        ];

        return $rule;
    }

    // 规则 - update 验证 more_json
    public function updateMoreJsonRule()
    {
        return parent::updateMoreJsonRule();
    }

    // model 使用 - 表单映射
    const FORM_FIELDS_MAP = [
        'id'        => 'id',
        'rank_num'  => 'rank_num',
        'is_enable' => 'is_enable',
        'remark'    => 'remark',
        'type'      => 'type',
        'file_id'   => 'file_id',
        'file_url'  => 'file_url',

        'login_name'    => 'login_name',
        'password'      => 'password',
        'user_type'     => 'user_type',
        'email'     => 'email',
        'phone'     => 'phone',
        'coin'      => 'coin',
        'birthday'  => 'birthday',
        'gender'    => 'gender',
        'more_json'    => 'more_json',
        'api_token' => 'api_token',
        'nickname'  => 'nickname',
        'qr_code_url'  => 'qr_code_url',
    ];

    // model 新增的搜索字段
    const ADDED_SEARCHABLE_FIELDS = [
        'email' => ['field' => 'email',  'op' => 'LIKE'],
        'phone' => ['field' => 'phone',  'op' => 'LIKE'],
        'user_type' => ['field' => 'user_type', 'op' => '='],
        'login_name'=> ['field' => 'login_name',  'op' => 'LIKE'],
    ];
}
